<?php
namespace Demo\Observe;
use SplObserver;
class User implements \SplSubject
{

    protected $observer = null;

    public function __construct()
    {
        $this->observer = new \SplObjectStorage();
    }

    public function login()
    {
        $this->notify();
        echo 'login success!'.PHP_EOL;
    }

    // 注册观察者
    public function attach(SplObserver $observer)
    {
        // TODO: Implement attach() method.
        $this->observer->attach($observer);
    }

    // 删除观察者
    public function detach(SplObserver $observer)
    {
        // TODO: Implement detach() method.
        $this->observer->detach($observer);
    }

    // 遍历执行观察者
    public function notify()
    {
        $this->observer->rewind();
        while ($this->observer->valid()){
            $observer = $this->observer->current();
            $observer->update($this);
            $this->observer->next();
        }
        // TODO: Implement notify() method.
    }
}